/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.b2b.modules.fm.web.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.b2b.common.utils.DateUtils;
import com.b2b.common.utils.MyBeanUtils;
import com.b2b.common.config.Global;
import com.b2b.common.persistence.Page;
import com.b2b.common.web.BaseController;
import com.b2b.common.utils.StringUtils;
import com.b2b.common.utils.excel.ExportExcel;
import com.b2b.common.utils.excel.ImportExcel;
import com.b2b.modules.fm.entity.base.FmAlarmconf;
import com.b2b.modules.fm.service.base.FmAlarmconfService;

/**
 * 报警参数Controller
 * @author soldier lee
 * @version 2016-11-20
 */
@Controller
@RequestMapping(value = "${adminPath}/fm/base/fmAlarmconf")
public class FmAlarmconfController extends BaseController {

	@Autowired
	private FmAlarmconfService fmAlarmconfService;
	
	@ModelAttribute
	public FmAlarmconf get(@RequestParam(required=false) String id) {
		FmAlarmconf entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = fmAlarmconfService.get(id);
		}
		if (entity == null){
			entity = new FmAlarmconf();
		}
		return entity;
	}
	
	/**
	 * 报警参数列表页面
	 */
	@RequiresPermissions("fm:base:fmAlarmconf:list")
	@RequestMapping(value = {"list", ""})
	public String list(FmAlarmconf fmAlarmconf, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FmAlarmconf> page = fmAlarmconfService.findPage(new Page<FmAlarmconf>(request, response), fmAlarmconf); 
		model.addAttribute("page", page);
		return "modules/fm/base/fmAlarmconfList";
	}

	/**
	 * 查看，增加，编辑报警参数表单页面
	 */
	@RequiresPermissions(value={"fm:base:fmAlarmconf:view","fm:base:fmAlarmconf:add","fm:base:fmAlarmconf:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(FmAlarmconf fmAlarmconf, Model model) {
		model.addAttribute("fmAlarmconf", fmAlarmconf);
		return "modules/fm/base/fmAlarmconfForm";
	}

	/**
	 * 保存报警参数
	 */
	@RequiresPermissions(value={"fm:base:fmAlarmconf:add","fm:base:fmAlarmconf:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(FmAlarmconf fmAlarmconf, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, fmAlarmconf)){
			return form(fmAlarmconf, model);
		}
		if(!fmAlarmconf.getIsNewRecord()){//编辑表单保存
			FmAlarmconf t = fmAlarmconfService.get(fmAlarmconf.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(fmAlarmconf, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			fmAlarmconfService.save(t);//保存
		}else{//新增表单保存
			fmAlarmconfService.save(fmAlarmconf);//保存
		}
		addMessage(redirectAttributes, "保存报警参数成功");
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
	}
	
	/**
	 * 删除报警参数
	 */
	@RequiresPermissions("fm:base:fmAlarmconf:del")
	@RequestMapping(value = "delete")
	public String delete(FmAlarmconf fmAlarmconf, RedirectAttributes redirectAttributes) {
		fmAlarmconfService.delete(fmAlarmconf);
		addMessage(redirectAttributes, "删除报警参数成功");
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
	}
	
	/**
	 * 批量删除报警参数
	 */
	@RequiresPermissions("fm:base:fmAlarmconf:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			fmAlarmconfService.delete(fmAlarmconfService.get(id));
		}
		addMessage(redirectAttributes, "删除报警参数成功");
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("fm:base:fmAlarmconf:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(FmAlarmconf fmAlarmconf, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报警参数"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<FmAlarmconf> page = fmAlarmconfService.findPage(new Page<FmAlarmconf>(request, response, -1), fmAlarmconf);
    		new ExportExcel("报警参数", FmAlarmconf.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报警参数记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("fm:base:fmAlarmconf:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<FmAlarmconf> list = ei.getDataList(FmAlarmconf.class);
			for (FmAlarmconf fmAlarmconf : list){
				try{
					fmAlarmconfService.save(fmAlarmconf);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条报警参数记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条报警参数记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入报警参数失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
    }
	
	/**
	 * 下载导入报警参数数据模板
	 */
	@RequiresPermissions("fm:base:fmAlarmconf:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报警参数数据导入模板.xlsx";
    		List<FmAlarmconf> list = Lists.newArrayList(); 
    		new ExportExcel("报警参数数据", FmAlarmconf.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/fm/base/fmAlarmconf/?repage";
    }
	
	
	

}